package com.eyas.business.controllor.classinfo;

import com.eyas.business.model.filepath.FileTmpPath;
import com.eyas.business.model.jdbcEntity.table.ClassInfo;
import com.eyas.business.model.pojo.classinfo.ClassInfoSaveDTO;
import com.eyas.business.service.classinfo.ClassInfoService;
import com.eyas.core.file.utils.FileUtils;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/5 13:15
 * @Description:
 */
@Controller
@RequestMapping("/eyas/classinfo/")
public class ClassInfoAction {
    @Autowired
    private ClassInfoService classInfoService;

    @GetMapping("/queryClassInfo")
    @ResponseBody
    public PageResult<ClassInfo> queryClassInfo(String classname, String validflag,
                                 @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "10") int limit) {
        PageResult<ClassInfo> classInfoPageResult =
                this.classInfoService.queryClassInfo(classname,validflag,page-1,limit);
        return classInfoPageResult;
    }

    @PostMapping("/changeValidflag")
    @ResponseBody
    public String changeValidflag(int classid) {
        this.classInfoService.changeValidflag(classid);
        return "{\"success\":true}";
    }

    @GetMapping("/edit")
    public String edit(int classid,Model model) {
        ClassInfo classInfo = this.classInfoService.getClassInfoByClassID(classid);
        model.addAttribute("classinfo",classInfo);
        return "/classinfo/edit";
    }

//    @GetMapping("/getImgcode")
    public void getImgcode(HttpServletResponse response,Integer classid) {
        response.setContentType("application/octet-stream");
        if(classid==null||classid==0)
            return;
        try {
            this.classInfoService.getImgcode(classid,response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.getOutputStream().flush();
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private FileTmpPath fileTmpPath;

    @RequestMapping("/uploadpic")
    @ResponseBody
    public String uploadpic(MultipartFile file) {
        //先暂存文件，表单提交的时候再保存到数据库中
        String tmpPath = fileTmpPath.getTmppath();
        String originalName = file.getOriginalFilename(); //文件名
        String suffex=originalName.substring(originalName.lastIndexOf(".")+1);  //文件后缀
        String fileName = new Date().getTime()+"_"+((int)(Math.random()*10000))+"."+suffex; //待保存的文件名
        String file_store_path = tmpPath+fileName;

        try {
            File file_store= FileUtils.createFile(file_store_path);
            file.transferTo(file_store);
        } catch (IOException e) {
            e.printStackTrace();
            return "{\"code\":1}";
        }

        return "{\"code\":0,\"url\":\""+file_store_path+"\",\"fileName\":\""+fileName+"\"}";
    }


    @PostMapping("/saveClassInfo")
    @ResponseBody
    public String saveClassInfo(ClassInfoSaveDTO dto) {
        this.classInfoService.saveClassInfo(dto);
        return "{\"code\":1}";
    }


    @GetMapping("/queryClassInfoList")
    @ResponseBody
    public List<ClassInfo> queryClassInfoList() {
        return this.classInfoService.queryClassInfoList(null);
    }

    @GetMapping("/listclassinfo")
    @ResponseBody
    public PageResult<ClassInfo> listClassInfo(Integer userid) {
        PageResult<ClassInfo> classInfoPageResult =
                this.classInfoService.listAllClass(userid);
        return classInfoPageResult;
    }
}
